package cc.wx.fom.ds;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建于:2015年7月29日<br>
 * 顶点实现
 * 
 * @author WangXue
 * @version 1.0.0
 */
public class Vertex implements IVertex {
	private String id;
	private List<IEdge> edges = new ArrayList<IEdge>();

	public Vertex(String id) {
		this.id = id;
	}

	@Override
	public String toString() {
		return "V" + id;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof IVertex) {
			IVertex vertex = (IVertex) obj;
			if (id.equals(vertex.getId())) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}

	@Override
	public String getId() {
		return id;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cc.wx.fom.ds.IVertex#getEdges()
	 */
	@Override
	public List<IEdge> getEdges() {
		return edges;
	}

	@Override
	public void addEdge(IEdge edge) {
		if (!edge.startWith(this) && !edge.endWith(this)) {
			throw new RuntimeException("边不合法");
		}
		if (!edge.startWith(this)) {
			// 给自己加边，必须要以自己为“头”
			edge = new Edge(edge.getTailVertex(), edge.getHeadVertex());
		}
		edges.add(edge);
	}

	@Override
	public void removeEdge(IEdge edge) {
		edges.remove(edge);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cc.wx.fom.ds.IVertex#getNearVertexs()
	 */
	@Override
	public List<IVertex> getNearVertexs() {
		List<IVertex> vertexs = new ArrayList<IVertex>();
		for (IEdge edge : edges) {
			IVertex vertex = edge.getTailVertex();
			vertexs.add(vertex);
		}
		return vertexs;
	}

}
